package com.ty.xsl.reader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;

import com.ty.xsl.field.ExcelField;
import com.ty.xsl.parserule.PortraitRule;
import com.ty.xsl.util.ExcelUtil;



	public class PortraitReader {

		public List<Map<String, Object>> read(PortraitRule rule, Sheet sheet){

			List<Map<String, Object>> dataset = new ArrayList<Map<String, Object>>();
			//得到filedList
			List<ExcelField> fields=rule.getFields();
			Map<String, Object> data = null;
			int offset=0;
			for (int j = 0; j<fields.size(); j++){

				ExcelField field = fields.get(j);
				if(field.getIndex() < 0){
					offset++;
					continue;
				}
				int colIndex = 0;
				int beginindex= field.getBeginIndex();
				int lastCellNum = sheet.getLastRowNum();
				for (int i=beginindex; i<=lastCellNum; i++){
					if (j == offset){
						data = new HashMap<String,Object>();
						dataset.add(data);
					}else {
						data = dataset.get(colIndex);
					}
					Cell cell = sheet.getRow(i).getCell(field.getIndex());
					String value = ExcelUtil.getStringCellValue(cell);
					data.put(field.getName(), value);

					colIndex++;
				}
			}
			return dataset;
		}
	}

